﻿Public Class Staff
    Private ms As Base.Entities.Moira()
    Private msWithEmpty As Base.Entities.Moira()
    Private stratef As Base.SortableCollection(Of Base.Entities.Staff)
    Private deleteThese As List(Of Integer)

    Private Sub Staff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ms = My.Connector.Client.Moires
        lbSquadrons.DataSource = ms
        lbSquadrons.DisplayMember = "Name"
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim selectedMoires As New List(Of Base.Entities.Moira)

        For Each i In lbSquadrons.SelectedItems
            selectedMoires.Add(i)
        Next

        stratef = My.Connector.Client.StaffSelection(selectedMoires.ToArray)

        dgvPersonnel.Columns.Clear()
        dgvPersonnel.DataSource = stratef
        dgvPersonnel.Columns("id").Visible = False
        dgvPersonnel.Columns.Remove(dgvPersonnel.Columns("Moira")) '.CellTemplate = New DataGridViewComboBoxCell
        Dim moiraColumn As New DataGridViewComboBoxColumn

        With moiraColumn
            .DataPropertyName = "Moira"
            .HeaderText = "Μοίρα"
            .DropDownWidth = 160
            .Width = 90
            .MaxDropDownItems = 4
            .FlatStyle = FlatStyle.Flat
            .DataSource = ms
            .ValueMember = "Id"
            .DisplayMember = "Description"
        End With

        deleteThese = New List(Of Integer)
        dgvPersonnel.Columns.Insert(dgvPersonnel.Columns.Count, moiraColumn)

    End Sub

    Private Sub dgvPersonnel_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPersonnel.CellValueChanged
        dgvPersonnel.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Bisque
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        My.Connector.Client.UpdateStaff(stratef, deleteThese.ToArray)
        btnLoad_Click(sender, e)
    End Sub

    Private Sub dgvPersonnel_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvPersonnel.UserDeletingRow
        Dim deleteMe As Base.Entities.Staff = CType(e.Row.DataBoundItem, Base.Entities.Staff)
        deleteThese.Add(deleteMe.Id)
    End Sub
End Class